/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MailSender;

import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author Windows7
 */
public class EmailSender {

    private String from;
    private String[] to;
    private String subject;
    private String content;
    private String password;

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String[] getTo() {
        return to;
    }

    public void setTo(String[] to) {
        this.to = to;
    }

    public EmailSender() {
    }

    public EmailSender(String from, String[] to, String subject, String content, String password) {
        this.from = from;
        this.to = to;
        this.subject = subject;
        this.content = content;
        this.password = password;
    }

    public void send() throws Exception {
        if (from == null) {
            throw new IllegalArgumentException("FROM ADDRESS is required");
        }
        if (to == null || to.length < 1) {
            throw new IllegalArgumentException("Must be atleast 1 TO ADDRESS");
        }
        if (password == null) {
            throw new IllegalArgumentException("Password is empty");
        }
        if (subject == null) {
            throw new IllegalArgumentException("Subject is required");
        }
        if (content == null) {
            throw new IllegalArgumentException("Content is required");
        }
        Properties properties = System.getProperties();

        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.starttls.enable", "true");
        properties.setProperty("mail.smtp.host", "smtp.gmail.com");

        // Get the default Session object.
        Session session = Session.getInstance(properties, new Authenticator() {

            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(from, password);
            }
        });
        try {

            // Create a default MimeMessage object.
            MimeMessage message = new MimeMessage(session);

            // Set From: header field of the header.
            message.setFrom(new InternetAddress(from));

            // Set To: header field of the header.
            for (String t : to) {
                message.addRecipient(Message.RecipientType.TO, new InternetAddress(t));
            }
            // Set Subject: header field
            message.setSubject(subject);

            // Now set the actual message
            message.setText(content, "UTF-8", "html");

            // Send message
            Transport.send(message);

        } catch (MessagingException ex) {
            throw ex;
        }
    }
}
